/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.market;

import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.dto.IncomingResources;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityResourcesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MarketManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityResourcesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MarketManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class CaravanPage extends WebPage{
    
    private MarketManager marketManager = new MarketManagerImpl();
    private CityResourcesManager cityResourcesManager = new CityResourcesManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private String selectedCity = "";
    private List<String> cities;

    public CaravanPage(PageParameters parameters) {

        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));


            cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(id)) {
                cities.add(c.getName());
            }
            if (parameters.containsKey("cityName")) {
                selectedCity = parameters.getString("cityName");
            }

            if (selectedCity.equals("")) {
                selectedCity = cities.get(0).toString();
            }

            p.add("cityName", selectedCity);

            add(new MarketPanel("marketPanel", p));           

            final DropDownChoice<String> city = new DropDownChoice<>("city",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            Form<?> selectCity = new Form<Void>("selectCity") {
                @Override
                protected void onSubmit() {
                    selectedCity = cities.get(Integer.parseInt(city.getValue()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("cityName", selectedCity);
                    info("Caravan has been sent.");
                    setResponsePage(CaravanPage.class, pageParameters);
                }
            };

            add(selectCity);
            selectCity.add(city);

            List<IncomingResources> coming = marketManager.getIncomingResourcesOfCity(cityManager.getCityByName(selectedCity).getId());

            ListView listview = new ListView("coming", coming) {
                @Override
                protected void populateItem(ListItem item) {

                    IncomingResources incomingResources = (IncomingResources) item.getModelObject();
                    item.add(new Label("from", cityManager.getCityById(incomingResources.getCityFrom()).getName()));
                    item.add(new Label("to", cityManager.getCityById(incomingResources.getCityTo()).getName()));
                    item.add(new Label("load", incomingResources.getResource().toString() + ": " + incomingResources.getResourceCount()));
                    item.add(new Label("arrival", incomingResources.getTimeOfArrival().toString()));
                    item.add(new Label("type", incomingResources.getMovementType().toString()));
                    
                }
            };
            add(listview);
        }
        
        List<IncomingResources> out = marketManager.getOutcomingResourcesOfCity(cityManager.getCityByName(selectedCity).getId());

            ListView listview = new ListView("sent", out) {
                @Override
                protected void populateItem(ListItem item) {

                    IncomingResources incomingResources = (IncomingResources) item.getModelObject();
                    item.add(new Label("from", cityManager.getCityById(incomingResources.getCityFrom()).getName()));
                    item.add(new Label("to", cityManager.getCityById(incomingResources.getCityTo()).getName()));
                    item.add(new Label("load", incomingResources.getResource().toString() + ": " + incomingResources.getResourceCount()));
                    item.add(new Label("arrival", incomingResources.getTimeOfArrival().toString()));
                    item.add(new Label("type", incomingResources.getMovementType().toString()));
                    
                }
            };
            add(listview);
    }
}
